﻿// RankingSejmowy 1.0 - Polish parliment statistics for the masses.
// Copyright (C) 2010 Jarosław Dubrownik
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System.Linq;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using RankingSejmowy.Model;

namespace RankingSejmowy.DAL.Deputies
{
    public class DeputyRepository : Repository<Deputy>, IDeputyRepository
    {
        #region IDeputyRepository Members

        public DeputyWithCount[] GetTopAbsentDeputies(int cadence, ISession session)
        {
            var criteria = GetAbsentCriteria(cadence, session)
                .SetMaxResults(10);
            return criteria.List<DeputyWithCount>().ToArray();
        }

        public DeputyWithCount[] GetTopPresentDeputies(int cadence, ISession session)
        {
            var criteria = GetPresentCriteria(cadence, session).SetMaxResults(10);
            return criteria.List<DeputyWithCount>().ToArray();
        }

        public DeputyWithCount[] GetTopAbsentPartyDeputies(int cadence, string partyName, ISession session)
        {
            var criteria = GetAbsentCriteria(cadence, session);
            criteria.CreateAlias("pc.Party", "p")
                .Add(Restrictions.Like("p.Name", partyName))
                .SetMaxResults(10);

            return criteria.List<DeputyWithCount>().ToArray();
        }

        public DeputyWithCount[] GetTopPresentPartyDeputies(int cadence, string partyName, ISession session)
        {
            var criteria = GetPresentCriteria(cadence, session);
            criteria.CreateAlias("pc.Party", "p")
                .Add(Restrictions.Like("p.Name", partyName))
                .SetMaxResults(10);

            return criteria.List<DeputyWithCount>().ToArray();
        }

        #endregion

        private ICriteria GetAbsentCriteria(int cadence, ISession session)
        {
            return GetCriteria(cadence, session).Add(Restrictions.Eq("Vote", EVote.None));
        }

        private ICriteria GetPresentCriteria(int cadence, ISession session)
        {
            return GetCriteria(cadence, session).Add(Restrictions.Not(Restrictions.Eq("Vote", EVote.None)));
        }

        private ICriteria GetCriteria(int cadence, ISession session)
        {
            return session.CreateCriteria<DeputyVote>("dv")
                // JOIN ...
                .CreateAlias("Deputy", "d")
                // JOIN ...
                .CreateAlias("d.PartyCadences", "pc")
                // JOIN ...
                .CreateAlias("pc.Cadence", "c")
                // SELECT ...
                .SetProjection(Projections.ProjectionList()
                                   .Add(Projections.GroupProperty("Id"), "Id")
                                   .Add(Projections.GroupProperty("d.Name"), "Name")
                                   .Add(Projections.RowCount(), "Count"))
                // WHERE ...
                .Add(Restrictions.Eq("c.CadenceNum", cadence))
                // ORDER BY Count DESC, d.LastName ASC
                .AddOrder(Order.Desc("Count"))
                .AddOrder(Order.Asc("d.Name"))
                .SetResultTransformer(Transformers.AliasToBean(typeof (DeputyWithCount)));
        }
    }
}